package com.hzsd.zengbanyi.biaodan_text.fragment;

import hzst.android.view.FlowLayout;



import android.annotation.SuppressLint;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.hzsd.zengbanyi.biaodan_text.R;
import com.hzsd.zengbanyi.biaodan_text.bean.LabSampleBean;

public class LabTopRightFragment extends Fragment implements OnClickListener{
	private View view;
	private RelativeLayout rl_ishiden;
	private TextView tv_ishiden;
	private TextView tv_AnalysisDate, tv_indoorT, tv_humidity;
	private TextView tv_SolutionName,tv_ColorT,tv_ColorTime,tv_ReferenceSolution,tv_wavelength,tv_Cuvette,tv_b,tv_a,tv_r,tv_date;
	private FlowLayout fl_fragment_lab_top;
	private LabSampleBean labSampleBean;
	private View dialogView;
	private boolean isHiden = true;
	private Context context;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		context = getActivity();
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_lab_top_right, container, false);
		baseData();
		initView(view);
		initEvents();
		return view;
	}

	private void baseData() {
		labSampleBean = (LabSampleBean) getArguments().getSerializable(
				"LabSampleBean");

	}
//daigsdiahfoahttps://git.oschina.net/zbyHML/biaodan_text.git
	private void initView(View view) {
		fl_fragment_lab_top = (FlowLayout) view.findViewById(R.id.fl_fragment_lab_top);
		rl_ishiden = (RelativeLayout) view.findViewById(R.id.rl_ishiden);
		tv_ishiden = (TextView) view.findViewById(R.id.tv_ishiden);
		
		tv_AnalysisDate = (TextView) view.findViewById(R.id.tv_AnalysisDate);
		tv_indoorT = (TextView) view.findViewById(R.id.tv_indoorT);
		tv_humidity = (TextView) view.findViewById(R.id.tv_humidity);
		
		tv_SolutionName = (TextView) view.findViewById(R.id.tv_SolutionName);
		tv_ColorT = (TextView) view.findViewById(R.id.tv_ColorT);
		tv_ColorTime = (TextView) view.findViewById(R.id.tv_ColorTime);
		tv_ReferenceSolution = (TextView) view.findViewById(R.id.tv_ReferenceSolution);
		tv_wavelength = (TextView) view.findViewById(R.id.tv_wavelength);
		tv_Cuvette = (TextView) view.findViewById(R.id.tv_Cuvette);
		tv_b = (TextView) view.findViewById(R.id.tv_b);
		tv_a = (TextView) view.findViewById(R.id.tv_a);
		tv_r = (TextView) view.findViewById(R.id.tv_r);
		tv_date = (TextView) view.findViewById(R.id.tv_date);

		refreshView();

	}

	private void initEvents() {
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_fragment_top_right, null, false);
				
				final TextView tv_AnalysisDate = (TextView) dialogView.findViewById(R.id.tv_AnalysisDate);
				final TextView tv_indoorT = (TextView) dialogView.findViewById(R.id.tv_indoorT);
				final TextView tv_humidity = (TextView) dialogView.findViewById(R.id.tv_humidity);
				final TextView et_SolutionName = (TextView) dialogView.findViewById(R.id.et_SolutionName);
				final TextView et_ColorT = (TextView) dialogView.findViewById(R.id.et_ColorT);
				final TextView et_ColorTime = (TextView) dialogView.findViewById(R.id.et_ColorTime);
				final TextView et_ReferenceSolution = (TextView) dialogView.findViewById(R.id.et_ReferenceSolution);
				final TextView et_wavelength = (TextView) dialogView.findViewById(R.id.et_wavelength);
				final TextView et_Cuvette = (TextView) dialogView.findViewById(R.id.et_Cuvette);
				final TextView et_b = (TextView) dialogView.findViewById(R.id.et_b);
				final TextView et_a = (TextView) dialogView.findViewById(R.id.et_a);
				final TextView et_r = (TextView) dialogView.findViewById(R.id.et_r);
				final TextView et_date = (TextView) dialogView.findViewById(R.id.et_date);
				tv_AnalysisDate.setText(labSampleBean.getAnalysisDate());
				tv_indoorT.setText(labSampleBean.getIndoorT());
				tv_humidity.setText(labSampleBean.getHumidity());
				et_SolutionName.setText(labSampleBean.getSolutionName());
				et_ColorT.setText(labSampleBean.getColorT());
				et_ColorTime.setText(labSampleBean.getColorTime());
				et_ReferenceSolution.setText(labSampleBean.getReferenceSolution());
				et_wavelength.setText(labSampleBean.getWavelength());
				et_b.setText(labSampleBean.getB());
				et_a.setText(labSampleBean.getA());
				et_r.setText(labSampleBean.getR());
				et_date.setText(labSampleBean.getDate());
				
				Builder builder = new Builder(getActivity());
				builder.setTitle("修改数据");
				builder.setView(dialogView);
				builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						String AnalysisDate = tv_AnalysisDate.getText().toString().trim();
						String indoorT = tv_indoorT.getText().toString().trim();
						String humidity = tv_humidity.getText().toString().trim();
						labSampleBean.setAnalysisDate(AnalysisDate);
						labSampleBean.setIndoorT(indoorT);
						labSampleBean.setHumidity(humidity);
						labSampleBean.setSolutionName(et_SolutionName.getText().toString());
						labSampleBean.setColorT(et_ColorT.getText().toString());
						labSampleBean.setColorTime(et_ColorTime.getText().toString());
						labSampleBean.setReferenceSolution(et_ReferenceSolution.getText().toString());
						labSampleBean.setWavelength(et_wavelength.getText().toString());
						labSampleBean.setB(et_b.getText().toString());
						labSampleBean.setA(et_a.getText().toString());
						labSampleBean.setR(et_r.getText().toString());
						labSampleBean.setDate(et_date.getText().toString());
						refreshView();
						dialog.dismiss();

					}
				});
				builder.create().show();
			}
		});
		
		rl_ishiden.setOnClickListener(this);

	}
	public void refreshView() {
		tv_AnalysisDate.setText(labSampleBean.getAnalysisDate());
		tv_indoorT.setText(labSampleBean.getIndoorT());
		tv_humidity.setText(labSampleBean.getHumidity());
		
		tv_SolutionName.setText(labSampleBean.getSolutionName());
		tv_ColorT.setText(labSampleBean.getColorT());
		tv_ColorTime.setText(labSampleBean.getColorTime());
		tv_ReferenceSolution.setText(labSampleBean.getReferenceSolution());
		tv_wavelength.setText(labSampleBean.getWavelength());
		tv_Cuvette.setText(labSampleBean.getCuvette());
		tv_b.setText(labSampleBean.getB());
		tv_a.setText(labSampleBean.getA());
		tv_r.setText(labSampleBean.getR());
		tv_date.setText(labSampleBean.getDate());
	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_ishiden:
			if (isHiden) {
				tv_ishiden.setBackground(context.getResources().getDrawable(R.mipmap.jiantou_d));
				fl_fragment_lab_top.setVisibility(View.VISIBLE);
				isHiden = false;
			}else{
				tv_ishiden.setBackground(context.getResources().getDrawable(R.mipmap.jiantou_r));
				fl_fragment_lab_top.setVisibility(View.GONE);
				isHiden = true;
			}
			break;

		default:
			break;
		}
		
	}
}
